package com.cyz.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cyz.pojo.User;
import com.cyz.service.ProductService;
import com.cyz.service.UserService;
import com.cyz.service.impl.ProductServiceImpl;
import com.cyz.service.impl.UserServiceImpl;

@Controller
@RequestMapping("/PersonInfo")
public class PersonInfo {
	@RequestMapping ("/personInfo")
	public ModelAndView  personInfo( HttpServletRequest request,HttpServletResponse response)
	{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("personInfo");
		mv.addObject("prePage",request.getSession().getAttribute("nowPage"));
		
		String userName=(String) request.getSession().getAttribute("userName");
		UserService userService=new UserServiceImpl();
		Integer userId=userService.getUserIdByUserName(userName);//获取用户id
		User user =new User();
		user=userService.getUserByName(userName);
		//productService.getProductById();
		mv.addObject("user",user);
		//mv.setViewName("product");
		
		return  mv;
	}
	@RequestMapping("/updateInfo")
	public void updateInfo(HttpServletRequest request,HttpServletResponse response)
	{
		String userName=(String) request.getSession().getAttribute("userName");
		UserService userService=new UserServiceImpl();
		Integer userId=userService.getUserIdByUserName(userName);//获取用户id
		String reUserName = request.getParameter("userName");
		String phone =request.getParameter("phone");
		String address=request.getParameter("address");		
	//	System.out.println(reUserName);
		UserService userService1 = new UserServiceImpl();
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			userService1.updateInfo(userId, reUserName, phone, address);
			printWriter.write("{\"res\":\"1\"}");
			}
		catch(Exception e)
		{
			//e1.printStackTrace();
			printWriter.write("{\"res\":\"2\"}");
		} finally {
			printWriter.close();
		}

	}
	

}
